package com.geline.sqlbackup.druid;

import cn.hutool.db.meta.JdbcType;
import com.alibaba.druid.DbType;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
import com.alibaba.druid.sql.ast.statement.SQLAlterTableAddConstraint;
import com.alibaba.druid.sql.ast.statement.SQLCommentStatement;
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor;
import com.alibaba.druid.sql.parser.SQLParserUtils;
import com.alibaba.druid.sql.parser.SQLStatementParser;
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
import com.alibaba.druid.util.JdbcConstants;
import com.geline.sqlbackup.bean.ColumnInfo;
import com.geline.sqlbackup.bean.TableColumnInfo;
import com.geline.sqlbackup.util.JavaPackageKit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * 人大金仓
 * @author mengx
 * @date 2020/12/27 10:28
 */
public class KingbaseDdlSqlHandler implements DdlSqlHandler {
    private static final Logger log = LoggerFactory.getLogger(KingbaseDdlSqlHandler.class);

    @Override
    public TableColumnInfo handler(String ddlSql, List<String> ignoredColumnList, boolean useRemoveTableHead, boolean useCamelCase) {
        TableColumnInfo tableColumnInfo = new TableColumnInfo();

        List<ColumnInfo> columnInfoList = new ArrayList<>();
        SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(ddlSql, JdbcConstants.KINGBASE);

        List<SQLStatement> sqlStatementList = parser.parseStatementList();
        for (SQLStatement sqlStatement : sqlStatementList){
            if (sqlStatement instanceof SQLCreateTableStatement) {
                SQLCreateTableStatement createTableStatement = (SQLCreateTableStatement) sqlStatement;
                //解析字段信息 create table 语句
                columnInfoList = initColumnInfoList(createTableStatement, ignoredColumnList, useCamelCase);

                //解析表名
                String tableName = createTableStatement.getTableName();
                tableName = tableName.replaceAll("\"|'|`", "").trim();
                tableColumnInfo.setTableName(tableName);

                //解析表注释
                SQLExpr comment = createTableStatement.getComment();
                String tableComment = comment==null ? "" : ((SQLCharExpr)comment).getText();
                tableColumnInfo.setTableComment(tableComment);

                //设置entity名
                String entityName = JavaPackageKit.getTableEntityName(useRemoveTableHead, useCamelCase, tableName);
                tableColumnInfo.setEntityName(entityName);
            }else if(sqlStatement instanceof SQLCommentStatement){
                log.warn(".....SQLCommentStatement.... "+sqlStatement);
            }else if(sqlStatement instanceof SQLAlterTableAddConstraint){
                log.warn(".....SQLAlterTableAddConstraint.... "+sqlStatement);
            }
        }

        tableColumnInfo.setColumnInfoList(columnInfoList);
        tableColumnInfo.setDbType(DbType.kingbase.name());
        return tableColumnInfo;
    }

    @Override
    public SchemaStatVisitor initSchemaStatVisitor() {
        return new OracleSchemaStatVisitor();
    }

    @Override
    public Integer getVendorTypeNumber(String columnName, String dataType) {
        if(dataType.equals("timestamptz")){
            return JdbcType.TIMESTAMP.typeCode;
        }else if(dataType.equals("int4")){
            return JdbcType.INTEGER.typeCode;
        }
        return -1;
    }

    @Override
    public String getJavaType(String columnName, String dataType) {
        return null;
    }

}
